import pymysql
from pymysql import Error
from loguru import logger
import time
# 数据库连接参数
# 数据库连接参数
host = 'rm-rj99x21x83424kxo5xo.mysql.rds.aliyuncs.com'
port = 3306  # 默认的MySQL端口
user = 'houjiajun'  # 替换为你的数据库用户名
password = 'J2i3a5j6u7n@'  # 替换为你的数据库密码
database = 'tag_all_sites'  # 替换为你的数据库名

def connect_sql():
    retry_count = 0
    while retry_count < 3:
        try:
            db = pymysql.connect(
                host=host,
                port=port,
                user=user,
                password=password,
                database=database,
                # charset='utf8mb4',
            )
            logger.info(f"连接成功")
            return db
        except Error as err:
            if err.args[0] == 2006 or err.args[0] == 2013:
                logger.error(f"连接失败，正在重新连接....")
                time.sleep(5)
                retry_count += 1
            else:
                logger.error(f"发生其他错误: {err}")
